<template>
  <div class='container'>
    <CardHeader>
      <template #left>
        <el-form :model="form" ref="inp" :inline="true">
          <el-form-item label="学科名称" prop="input">
            <el-input v-model="form.input"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="reset">清除</el-button>
            <el-button size="small" type="primary" @click="search">搜索</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #right>
        <el-button size="small" type="success" @click="onAdd" icon="el-icon-edit">新增学科</el-button>
      </template>
    </CardHeader>

    <el-alert
    :title="'数据一共'+counts+'条'"
    type="info"
    :closable="false"
    show-icon>
  </el-alert>

  <!-- 表格 -->
   <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{background:'#fafafa'}"
      >
      <el-table-column
      type="index"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="学科名称"
        width="145">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期"
        width="160"
        >
        <template slot-scope="{row}">
          {{row.addDate | parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="前台是否显示">
        <template slot-scope="{row}">
          {{row.isFrontDisplay ? '是':'否'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="twoLevelDirectory"
        label="二级目录">
      </el-table-column>
      <el-table-column
        prop="tags"
        label="标签">
      </el-table-column>
      <el-table-column
        prop="totals"
        label="题目数量">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        width="240">
        <template slot-scope="{row}">
        <el-button type="text" @click="$router.push({path:'directorys',query:{id:row.id,name:row.subjectName}})">学科分类</el-button>
        <el-button type="text" @click="$router.push({path:'tags',query:{id:row.id,name:row.subjectName}})">学科标签</el-button>
        <el-button type="text" @click="editSubject(row)">修改</el-button>
        <el-button type="text" @click="deleteSubject(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  <!-- 分页 -->
    <Pagination :counts='counts' @changePageSize='changePageSize' @changeCurrentPage='changeCurrentPage' :page='{currentPage:1,pageSize:10}'/>

    <!-- 新增修改对话框 -->
    <el-dialog
      :title="DialogForm.id?'修改学科':'新增学科'"
      :visible.sync="isShowDialog"
      @closed='close'
      width="30%">
      <el-form :rules="rules" ref="DialogForm" :model="DialogForm" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model.trim="DialogForm.subjectName" placeholder="请输入学科名称"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
            v-model="DialogForm.isFrontDisplay"
            :active-value=1
            :inactive-value=0
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      isShowDialog: false, // 新增对话框是否显示
      form: {
        input: '' // 搜索框值
      }, // 表单数据
      tableData: [], // 表格数据
      counts: 0, // 总条数
      pageForm: {
        page: 1, // 当前页码
        pagesize: 10, // 每页显示数量
        subjectName: ''// 学科名称
      }, // 获取表单所需数据
      DialogForm: {
        subjectName: '', // 学校名称
        isFrontDisplay: 1 // 开关按钮
      }, // 对话框数据
      rules: {
        subjectName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }// 表单验证
    }
  },
  created () {
    this.loadTableInfo()
  },
  methods: {
    // 加载表格数据
    async loadTableInfo () {
      const res = await list(this.pageForm)
      this.counts = res.data.counts
      this.tableData = res.data.items
      console.log(res)
    },
    // 重置输入框
    reset () {
      this.$refs.inp.resetFields()
      this.pageForm.subjectName = ''
      this.loadTableInfo()
    },
    // 点击搜索
    search () {
      this.pageForm.subjectName = this.form.input
      this.loadTableInfo()
    },
    // 改变每页数量
    changePageSize (e) {
      this.pageForm.pagesize = e
      this.loadTableInfo()
    },
    // 改变页码
    changeCurrentPage (e) {
      this.pageForm.page = e
      this.loadTableInfo()
    },
    // 关闭对话框
    close () {
      this.isShowDialog = false
      this.DialogForm = {
        subjectName: '', // 学校名称
        isFrontDisplay: 1 // 开关按钮
      }
    },
    // 保存设置
    async onSave () {
      if (this.DialogForm.id) {
        await update(this.DialogForm)
        this.$message.success('修改学科成功')
        this.close()
        this.loadTableInfo()
      } else {
        await add(this.DialogForm)
        this.$message.success('新增学科成功')
        this.isShowDialog = false
        this.close()
        this.loadTableInfo()
      }
    },
    // 点击修改按钮
    async editSubject (e) {
      this.DialogForm = { ...e }
      this.isShowDialog = true
    },
    // 新增学科
    onAdd () {
      this.isShowDialog = true
      this.DialogForm = {
        subjectName: '', // 学校名称
        isFrontDisplay: 1 // 开关按钮
      }
    },
    // 删除按钮
    deleteSubject (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadTableInfo()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.container{
  padding: 20px;
  background-color: #fff;
}
.el-alert{
  margin-bottom: 15px;
}
</style>
